//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    userInfo: {},
    userinfo: {},
    nickName:"",
    photoUrl: '',
    // token: '',
    varCreditNo: '',
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  onLoad: function () {
    var that = this
    var user = wx.getStorageSync('user')
    var userInf = JSON.parse(user).user
    var tokenInf = JSON.parse(user).token
    that.setData({  
      userinfo: userInf
    })
    // console.log(userInf)
    this.getCreditNo()
    this.getAvatar()
  },
  //获取社会信用代码
  getCreditNo(){
    var that = this
    wx.getStorage({
      key: 'user',
      success(res) {
        wx.request({
          url: app.globalData.Url+"/emergency/v1.0/company/info", 
          method: 'POST',
          header: {
            'content-type': 'application/json', // 默认值
            'Token': JSON.parse(res.data).token,
            'userId': JSON.parse(res.data).user.id
          },
          data: {
            companyId: JSON.parse(res.data).user.companyId,

          },
          success(res) {
            // console.log(res)
            if(res.data.meta.success){
              if (res.data.data.varCreditNo){
                that.setData({
                  varCreditNo: res.data.data.varCreditNo
                })
              }         
            }else{
              wx.showToast({
                title: res.data.meta.message,
                icon: 'none',
                duration: 1000,
                mask: true
              });
            }
          }
        })
      }
    })  
  },
  //获取头像
  getAvatar() {
    var that = this
    wx.getStorage({
      key: 'user',
      success(res) {
        wx.request({
          url: app.globalData.Url+"/emergency/v1.0/personal/info",
          method: 'POST',
          header: {
            'content-type': 'application/json', // 默认值
            'Token': JSON.parse(res.data).token,
            'userId': JSON.parse(res.data).user.id
          },
          data: {
            userId: JSON.parse(res.data).user.id
          },
          success(res) {
            // console.log(res)
            if (res.data.meta.success) {
              if (res.data.data.photo){
                that.setData({
                  photoUrl: res.data.data.photo
                })
              }else{
                that.setData({
                  photoUrl: '/images/person.png'
                })          
              }      
            } else {
              wx.showToast({
                title: res.data.meta.message,
                icon: 'none',
                duration: 3000,
                mask: true
              });
            }
          }
        }) 
      }
    })     
  },
  onTabItemTap(){
    this.setData({
      userInfo: app.globalData.userInfo
    });
  },
  searchname(event){
     console.log(event.detail)
  },
  linkpersonto: function () {
    wx.navigateTo({
      url: '../linkperson/index'
    })
  },
  feedbackfun(){
    wx.navigateTo({
      url: '../feedback/index'
    })
  },
  goback(){
    wx.navigateBack({
      delta: -1
    })
  }
})
